home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Best of MacTutor - S…e Code for Volumes 1 to 5
/
The Best of MacTutor - Source Code for Volume 1-5 (Wayzata Technology)(6031)(1990).bin
/
Source Code
/
#43 (Apr 89)
/
Designer CDEF Code
/
ExampleWindow.Pas
< prev
next >
Wrap
Pascal/Delphi Source File
|
1989-01-09
|
11KB
|
294 lines
UNIT ExampleWindow;
{File name: ExampleWindow.Pas}
{Function: Handle a Window}
{History: 12/19/88 Original by Prototyper. }
{Modified 1/6/89 optimized a little}
INTERFACE
USES
myCDEF;
{Initialize us so all our routines can be activated}
PROCEDURE Init_ExampleWindow;
{Open our window and draw everything}
PROCEDURE Open_ExampleWindow (VAR theWindow : WindowPtr);
{Update our window, someone uncovered a part of us}
PROCEDURE Update_ExampleWindow (whichWindow : WindowPtr);
{Handle action to our window, like controls}
PROCEDURE Do_ExampleWindow (myEvent : EventRecord);
IMPLEMENTATION
CONST
I_Buttonx1 = 3; {Button ID}
I_Checkboxx2 = 4; {Checkbox ID}
I_Radio2 = 6; {Radio ID}
I_Radio3 = 7; {Radio ID}
I_Radio1 = 5; {Radio ID}
VAR
MyWindow : WindowPtr; {Window pointer}
tempRect : Rect; {Temporary rectangle}
CtrlHandle, buttonHdl : controlhandle; {Control handle}
sTemp : Str255; {Get text entered, temp holding}
R1Control : ARRAY[1..3] OF ControlHandle; { Radio button handles for group 1}
{=================================}
{Initialize us so all our routines can be activated}
PROCEDURE Init_ExampleWindow;
BEGIN {Start of Window initialize routine}
MyWindow := NIL; {Make sure other routines know we are not valid yet}
END; {End of procedure}
{=================================}
{Update our window, someone uncovered a part of us}
PROCEDURE UpDate_ExampleWindow;
CONST
picBaseID = 144;
VAR
SavePort : WindowPtr; {Place to save the last port}
pictRect : rect;
Pic_Handle : PicHandle;
pictindex : integer;
BEGIN {Start of Window update routine}
IF (MyWindow <> NIL) AND (MyWindow = whichWindow) THEN {Handle an open when already opened}
BEGIN
GetPort(SavePort); {Save the current port}
SetPort(MyWindow); {Set the port to my window}
TextFont(systemFont); {Set the font to draw in}
{Draw a string of text, Static Text }
SetRect(tempRect, 266, 100, 312, 116);
sTemp := 'Button';
TextBox(Pointer(ord(@sTemp) + 1), length(sTemp), tempRect, teJustLeft);
{Draw a string of text, Static Text }
SetRect(tempRect, 151, 100, 217, 116);
sTemp := 'CheckBox';
TextBox(Pointer(ord(@sTemp) + 1), length(sTemp), tempRect, teJustLeft);
{Draw a string of text, Static Text }
SetRect(tempRect, 36, 100, 77, 116);
sTemp := 'Radio';
TextBox(Pointer(ord(@sTemp) + 1), length(sTemp), tempRect, teJustLeft);
{Draw a string of text, Static Text }
SetRect(tempRect, 76, 125, 287, 141);
sTemp := 'Designer CDEF by Kirk Chase';
TextBox(Pointer(ord(@sTemp) + 1), length(sTemp), tempRect, teJustLeft);
TextFont(applFont); {Set the default application font}
pictRect := buttonHdl^^.contrlRect;
InsetRect(pictRect, 2, 2);
pictindex := GetCtlValue(buttonHdl);
Pic_Handle := GetPicture(picBaseID + pictindex); {Get Picture into memory}
IF (Pic_Handle <> NIL) THEN {Only use handle if it is valid}
DrawPicture(Pic_Handle, pictRect);
DrawControls(MyWindow); {Draw all the controls}
SetPort(SavePort); {Restore the old port}
END; {End for if (MyWindow<>nil)}
END; {End of procedure}
{=================================}
{Open our window and draw everything}
PROCEDURE Open_ExampleWindow;
VAR
CDEFHandle : handle;
chkhdl : controlhandle;
BEGIN {Start of Window open routine}
IF (MyWindow = NIL) THEN {Handle an open when already opened}
BEGIN
MyWindow := GetNewWindow(1, NIL, Pointer(-1)); {Get the window from the resource file}
theWindow := MyWindow;
{get a handle to our CDEF Proc}
CDEFHandle := NewHandle(0);
CDEFHandle^ := Ptr(@DesignerButtonProc);
{ Make a button, Button }
CtrlHandle := GetNewControl(I_Buttonx1, MyWindow);
HLock(Handle(CtrlHandle));
CtrlHandle^^.contrlDefProc := CDEFHandle; {give in our handle}
HUnLock(Handle(CtrlHandle));
CtrlHandle^^.contrlHilite := 255;
buttonHdl := CtrlHandle;
{ Make a checkbox, Checkbox }
CtrlHandle := GetNewControl(I_Checkboxx2, MyWindow); {Make a new checkbox}
HLock(Handle(CtrlHandle));
CtrlHandle^^.contrlDefProc := CDEFHandle; {give in our handle}
HUnLock(Handle(CtrlHandle));
chkhdl := CtrlHandle;
{ Make a radio button, Radio1 }
R1Control[1] := GetNewControl(I_Radio1, MyWindow);
HLock(Handle(CtrlHandle));
R1Control[1]^^.contrlDefProc := CDEFHandle; {give in our handle}
HUnLock(Handle(CtrlHandle));
{ Make a radio button, Radio2 }
R1Control[2] := GetNewControl(I_Radio2, MyWindow);
HLock(Handle(CtrlHandle));
R1Control[2]^^.contrlDefProc := CDEFHandle; {give in our handle}
HUnLock(Handle(CtrlHandle));
{ Make a radio button, Radio3 }
R1Control[3] := GetNewControl(I_Radio3, MyWindow);
HLock(Handle(CtrlHandle));
R1Control[3]^^.contrlDefProc := CDEFHandle; {give in our handle}
HUnLock(Handle(CtrlHandle));
ShowWindow(MyWindow);
SelectWindow(MyWindow); {Bring our window to the front}
SetPort(MyWindow); {Prepare to write into our window}
UpDate_ExampleWindow(MyWindow); {Do an update to draw rest of items}
END {End for if (MyWindow<>nil)}
ELSE
SelectWindow(MyWindow); {Already open, so show it}
END; {End of procedure}
{=================================}
{Handle action to our window, like controls}
PROCEDURE Do_ExampleWindow;
CONST
HiliteValue = 10;
inactive = 255;
active = 0;
left = 0;
right = 1;
VAR
RefCon : integer; {RefCon for controls}
code : integer; {Location of event in window or controls}
theValue : integer; {Current value of a control}
whichWindow : WindowPtr; {Window pointer where event happened}
myPt, myPtGlobal : Point; {Point where event happened}
theControl : ControlHandle; {Handle for a control}
badRect : rect;
PROCEDURE Do_A_Button; {Handle a button being pressed}
BEGIN
HiliteControl(theControl, HiliteValue); {Darken the button}
RefCon := GetCRefCon(theControl); {get control refcon}
CASE RefCon OF {Select correct button}
I_Buttonx1 : {Button, button}
BEGIN {start for this button}
theValue := GetCtlValue(theControl); {Get current value}
theValue := (theValue + 1) MOD 6; {Change value}
SetCtlValue(theControl, theValue); {Set button to new value}
badRect := buttonhdl^^.contrlRect;
InvalRect(badRect); {invalidate for new die picture}
END; {end for this button}
OTHERWISE {allow other buttons, trap for debug}
;
END; {end of case}
HiliteControl(theControl, active); {Lighten the button}
END; {Handle a button being pressed}
PROCEDURE Do_A_Checkbox; {Handle a checkbox being pressed}
VAR
Index : integer; {Index used for radios}
PROCEDURE Clear1RadioGroup; {Routine to clear radios in group 1}
VAR
Index : integer; {Index used for radios}
BEGIN {Start of the clear routine}
FOR Index := 1 TO 3 DO {Step thru all radios}
SetCtlValue(R1Control[Index], 0); {Set this radio to zero}
END; {End of the clear routine}
BEGIN {Handle a checkbox being pressed}
RefCon := GetCRefCon(theControl); {get control refcon}
theValue := GetCtlValue(theControl); {Get current value}
theValue := (theValue + 1) MOD 2; {Change value}
CASE RefCon OF {Select correct button}
I_Checkboxx2 : {Checkbox, checkbox}
BEGIN {start for this button}
SetCtlValue(theControl, theValue); {Set checkbox to new value}
badRect := buttonhdl^^.contrlRect;
InvalRect(badRect);
CASE theValue OF {activate left or right controls}
left :
BEGIN
HiliteControl(buttonHdl, inactive);
HiliteControl(R1Control[1], active);
HiliteControl(R1Control[2], active);
HiliteControl(R1Control[3], active);
END;
right :
BEGIN
HiliteControl(buttonHdl, active);
HiliteControl(R1Control[1], inactive);
HiliteControl(R1Control[2], inactive);
HiliteControl(R1Control[3], inactive);
END;
OTHERWISE
;
END;
END; {end for this checkbox}
I_Radio1, I_Radio2, I_Radio3 : {radio buttons}
BEGIN {start for this radio button}
Clear1RadioGroup; {Clear all Radio values in this group}
SetCtlValue(theControl, 1); {Select this Radio}
END; {end for this radio button}
OTHERWISE
;
END; {end of case}
END; {Handle a checkbox being pressed}
BEGIN {Start of Window handler}
IF (MyWindow <> NIL) THEN {Handle only when the window is valid}
BEGIN
code := FindWindow(myEvent.where, whichWindow); {Get where in window and which window}
IF (myEvent.what = MouseDown) AND (MyWindow = whichWindow) THEN {}
BEGIN {}
myPt := myEvent.where; {Get mouse position}
myPtGlobal := myPt;
GlobalToLocal(myPt);
END;
IF (MyWindow = whichWindow) AND (code = inContent) THEN {for our window}
BEGIN
code := FindControl(myPt, whichWindow, theControl); {Get type of control}
IF (code <> 0) THEN {Check type of control}
code := TrackControl(theControl, myPt, NIL); {Track the control}
IF code = inButton THEN
Do_A_Button; {Do buttons}
IF code = inCheckBox THEN
Do_A_Checkbox; {Do checkboxes}
END; {End for if (MyWindow=whichWindow)}
END; {End for if (MyWindow<>nil)}
END; {End of procedure}
{=================================}
END. {End of unit}